package com.yc.example.view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import com.yc.example.bean.Msg;
import com.yc.example.controller.UserController;


public class IndexJFrame extends JFrame{
	
	private LoginJPanel loginJPanel = new LoginJPanel();
	
	
	public static void main(String[] args){
		
		new IndexJFrame("demo").setVisible(true);
       
	}

	public IndexJFrame(String title) throws HeadlessException {
		super(title);
		// TODO Auto-generated constructor stub
		setSize(850, 550);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setLocationRelativeTo(null);
		
		loginJPanel.getBtn_login().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String userName = loginJPanel.getJta_text().getText();
				String password = loginJPanel.getJtb_text().getText();

				loginJPanel.setUserController(new UserController());
				Msg msg = loginJPanel.getUserController().login(userName, password);
				
				if(msg.getResult().equals("登录成功")){
				
					System.out.println(msg);
					remove(loginJPanel);
					getContentPane().add(new IndexJPanel(msg.getResult()));
					getContentPane().validate();
				
				} else{
					
					JOptionPane.showMessageDialog(null, msg.getResult(),null,JOptionPane.ERROR_MESSAGE,null);
					loginJPanel.getJta_text().setText("");
					loginJPanel.getJtb_text().setText("");
				}
			}
		});
		
		getContentPane().add(loginJPanel);
	}

}


